function bestCharge(selectedItems) {
  let Allitems=require('./items');
  let Promotions=require('./promotions');
  let cartItems=[];
  let object={};
  //let selectedItems = ["ITEM0013 x 4"];
  
  for(let i of selectedItems){
    let arr=i.split(" x ");
    object[arr[0]]={};
    object[arr[0]].id=arr[0];
    object[arr[0]].count=arr[1];
  }
  for(let i in object){
    cartItems.push(object[i]);
  } 
  //console.log(cartItems);
  let allitems=Allitems();
  let promotions=Promotions();
  let orderBills=[];
  let object1={};
  for(let i of cartItems){
    for(let j of allitems){
      if(i.id==j.id){
        object1[i.id]={};
        object1[i.id].id=i.id;
        object1[i.id].name=j.name;
        object1[i.id].count=i.count;
        object1[i.id].allPrice=i.count*j.price;
      }
    }
  }
  for(let i in object1){
    orderBills.push(object1[i]);
  }
  console.log(orderBills);
  let sumprice=0;
  for(let i of orderBills){
    sumprice+=i.allPrice;
  }
  let planA=0;
  for(let i of orderBills){
    planA+=i.allPrice;
  }
  if(planA>=30){
    planA-=6;
  }
  let planB=0,promotionCharge=0,promotionItems=[];
  for(let i of orderBills){
    for(let j of promotions[1].items){
      if(i.id==j){
        promotionItems.push(i.name);
        promotionCharge+=i.allPrice/2;
      }
    }
  }
  planB=sumprice-promotionCharge;
  let bestCharge=0;
  let result;
  if(sumprice<30&&promotionItems.length==0){
    result=
    '============= 订餐明细 =============\n'+
    orderBills[0].name+' x '+orderBills[0].count+' = '+orderBills[0].allPrice+'元\n'+
    //orderBills[1].name+' x '+orderBills[1].count+' = '+orderBills[1].allPrice+'元\n'+
    //orderBills[2].name+' x '+orderBills[2].count+' = '+orderBills[2].allPrice+'元\n'+
    '-----------------------------------\n'+
    '总计：'+sumprice+'元\n'+
    '===================================\n';
  }
  else {
    if(planA<=planB){
      bestCharge=planA;
      result=
      '============= 订餐明细 =============\n'+
      orderBills[0].name+' x '+orderBills[0].count+' = '+orderBills[0].allPrice+'元\n'+
      orderBills[1].name+' x '+orderBills[1].count+' = '+orderBills[1].allPrice+'元\n'+
      //orderBills[2].name+' x '+orderBills[2].count+' = '+orderBills[2].allPrice+'元\n'+
      '-----------------------------------\n'+
      '使用优惠:\n'+
      promotions[0].type+'，'+'省6元\n'+
      '-----------------------------------\n'+
      '总计：'+bestCharge+'元\n'+
      '===================================\n';
    }
    else {
      bestCharge=planB;
      result=
      '============= 订餐明细 =============\n'+
      orderBills[0].name+' x '+orderBills[0].count+' = '+orderBills[0].allPrice+'元\n'+
      orderBills[1].name+' x '+orderBills[1].count+' = '+orderBills[1].allPrice+'元\n'+
      orderBills[2].name+' x '+orderBills[2].count+' = '+orderBills[2].allPrice+'元\n'+
      '-----------------------------------\n'+
      '使用优惠:\n'+
      promotions[1].type+'('+promotionItems[0]+'，'+promotionItems[1]+')'+'，'+'省'+promotionCharge+'元\n'+
      '-----------------------------------\n'+
      '总计：'+bestCharge+'元\n'+
      '===================================\n';
    }
  }
  return result; 
}
module.exports=bestCharge;